package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.HomeAdvertise;
import cn.hx.mall.sell.service.HomeAdvertiseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 首页轮播广告表(SmsHomeAdvertise)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:31
 */
@Controller
@RequestMapping("sell")
@MultipartConfig
public class HomeAdvertiseController {
    /**
     * 服务对象
     */
    @Resource
    private HomeAdvertiseService homeAdvertiseService;

    /*页面渲染*/
    @GetMapping("advertise")
    public String selectAll(Model model, HomeAdvertise homeAdvertise,
                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "5") Integer size) {
        return getString(homeAdvertise, model, page, size);
    }

    /*模糊查询*/
    @PostMapping("likeAll")
    public String likeAll(HomeAdvertise homeAdvertise, Model model,
                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size
    ) {
        return getString(homeAdvertise, model, page, size);
    }

    private String getString(HomeAdvertise homeAdvertise, Model model, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<HomeAdvertise> homeAdvertises = PageHelper.startPage(page, size);
        homeAdvertiseService.queryAll(homeAdvertise);
        PageInfo<HomeAdvertise> pageInfo = new PageInfo<>(homeAdvertises);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("search", homeAdvertise.getTitle());
        return "sell/advertise";
    }

    /*跳转修改页面*/
    @GetMapping("advertise-update")
    public String update(Model model) {
        List<HomeAdvertise> homeAdvertises = homeAdvertiseService.selectAll();
        model.addAttribute("advertiseList", homeAdvertises);
        return "sell/advertise-update";
    }

    /*编辑回填*/
    @GetMapping("updateFindById/{id}")
    public String updateFindById(Model model, @PathVariable Integer id) {
        HomeAdvertise homeAdvertise = homeAdvertiseService.queryById(id);
        model.addAttribute("advertise", homeAdvertise);
        return "sell/advertise-update";
    }


    /*状态按钮改变*/
    @GetMapping("statusById/{id}")
    @ResponseBody
    public Map<String,Object> statusById(@PathVariable Integer id) {
        HomeAdvertise homeAdvertise = homeAdvertiseService.queryById(id);
        Integer status = homeAdvertise.getStatus();
        if (status.equals(1)) {
            homeAdvertise.setStatus(0);
        } else {
            homeAdvertise.setStatus(1);
        }
        HashMap<String, Object> map = new HashMap<>();
        HomeAdvertise update = homeAdvertiseService.update(homeAdvertise);
        if(update!=null)
            map.put("code",1);
        return map;
    }


    /*根据ID删除*/
    @GetMapping("deleteById/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        boolean b = homeAdvertiseService.deleteById(id);
        if(b){
            map.put("code",1);
        }
        return map;
    }



    /*编辑页面提交*/
    @PostMapping("tool")
    public String tool(HomeAdvertise homeAdvertise, MultipartFile file) throws IOException {
        String oldName = file.getOriginalFilename();
        String type = oldName.substring(oldName.lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        file.transferTo(new File("D:\\Spring\\mall\\src\\main\\webapp\\assets\\images\\sell\\" + uuid + type));
        homeAdvertise.setPic("/mall/assets/images/sell/" + uuid + type);
        if (homeAdvertise.getId() == null) {
            homeAdvertiseService.insert(homeAdvertise);
        } else if (homeAdvertise.getId()!=null){
            homeAdvertiseService.update(homeAdvertise);
        }
        return "redirect:/sell/advertise";
    }

}